#include<vector>
using namespace std;
class Solution {
public:
    int numberOfAlternatingGroups(vector<int>& colors) {
        int left = 0, middle = 1, right = 2;
        int count = 0, n = colors.size();
        while (left < n)
        {
            if (middle == n - 1)
            {
                if (colors[middle] != colors[left] && colors[middle] != colors[0])
                    count++;
            }
            else if (left == n - 1)
            {
                if (colors[0] != colors[left] && colors[0] != colors[1])
                    count++;
            }
            else if (colors[middle] != colors[left] && colors[middle] != colors[right])
                count++;


            left++;
            middle++;
            right++;
        }
        return count;
    }
};